package com.onLineCar.manages.service.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.onLineCar.manages.api.dto.ApiVehicleBaseInfo;
import com.onLineCar.manages.api.dto.ApiVehicleInfoDTO;
import com.onLineCar.manages.pojo.China;
import com.onLineCar.manages.pojo.ImageTable;
import com.onLineCar.manages.pojo.VehicleInfo;
import com.onLineCar.manages.pojo.mapstruct.IPersonMapper;
import com.onLineCar.manages.service.BaseService;
import com.onLineCar.manages.service.ImageTableService;
import com.onLineCar.manages.service.VehicleInfoService;
import com.onLineCar.manages.service.api.ApiVehicleInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.onLineCar.manages.pojo.imageenum.ImageEnum.CAR_DRIVING_LICENSE_PHOTO;

/**
 * <p>
 * 车辆基本信息表 服务实现类
 * </p>
 *
 * @author zhucaiyi
 * @since 2023-12-10
 */
@Service
@Slf4j
public class ApiVehicleInfoServiceImpl implements ApiVehicleInfoService {


    @Resource
    private VehicleInfoService vehicleInfoService;

    @Resource
    private BaseService baseService;

    @Resource
    private ImageTableService imageService;

    @Override
    public List<ApiVehicleInfoDTO> queryAllVehicleInfo() {
        return ApiVehicleInfoDTO.convertToVehicleInfoDTOList(vehicleInfoService.list());
    }

    @Override
    public List<ApiVehicleInfoDTO> queryVehicleInfoByVehicleIds(List<Integer> vehicleIds) {
        return ApiVehicleInfoDTO.convertToVehicleInfoDTOList(vehicleInfoService.listByIds(vehicleIds));
    }

    @Override
    public Integer supportCity(String district) {
        LambdaQueryWrapper<China> chinaLambdaQueryWrapper = new LambdaQueryWrapper<>();
        chinaLambdaQueryWrapper.eq(China::getName, district).select(China::getId);
        China china = baseService.getOne(chinaLambdaQueryWrapper);
        return china.getId();
    }

    @Override
    public Boolean driverService(String district) {
        try {
            log.info("模拟查询本城市是否有可用司机");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    @Override
    public Boolean havaBillingRule(String district) {
        try {
            log.info("模拟查询本城市是否有可用计费规则");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    @Override
    public List<ApiVehicleBaseInfo> selectAllVehicleBaseInfo() {
        LambdaQueryWrapper<VehicleInfo> vehicleInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        vehicleInfoLambdaQueryWrapper.eq(VehicleInfo::getIsDeleted, NumberUtils.INTEGER_ZERO);
        vehicleInfoLambdaQueryWrapper.eq(VehicleInfo::getVehicleStatus, NumberUtils.INTEGER_ZERO);
        List<VehicleInfo> vehicleInfoList = vehicleInfoService.list(vehicleInfoLambdaQueryWrapper);
        List<Integer> vehiclelist = vehicleInfoList.stream().map(VehicleInfo::getVehicleId).toList();
        List<ImageTable> imageTables = imageService.queryImageTableByBusinessIdsAndImageCode(vehiclelist, CAR_DRIVING_LICENSE_PHOTO.imageType);
        List<ApiVehicleBaseInfo> apiVehicleBaseInfos = IPersonMapper.INSTANCT.covertToApiVehicleBaseInfo(vehicleInfoList);
        apiVehicleBaseInfos.forEach(apiVehicleBaseInfo -> {
            imageTables.stream().filter(imageTable -> apiVehicleBaseInfo.getVehicleId().equals(imageTable.getBusinessId()))
                    .forEach(imageTable -> apiVehicleBaseInfo.setImageUrl(imageTable.getImageUrl()));
        });
        return apiVehicleBaseInfos;
    }
}
